﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace MOJsdk
{
    public class MOJLocalization
    {
        public MOJLocalization()
        {
        }

        public static string Localize(string QUERY)
        {
            IHost _host = MojHost.GetHost;
            try
            {
                if (MojEnviromentVarsEnum.LanguageFile.Value() == "DEFAULT")
                    return QUERY;

                string langPath = Path.Combine(MojFoldersEnum.LanguagesFolder, MojEnviromentVarsEnum.LanguageFile.Value() + ".xml");
                XDocument doc = XDocument.Load(langPath);
                List<XElement> _elements = new List<XElement>(doc.Element("Language").Elements("Local"));

                if (_elements.Where(data => data.Attribute("def").Value == QUERY).Count() == 1)
                    return _elements.First(data => data.Attribute("def").Value == QUERY).Value;
                else
                    return QUERY;
            }
            catch (Exception exception)
            {
                _host.ReportError(new ErrorReporter()
                {
                    ErrorCode = "LOCALIZATIONx01",
                    ErrorDetails = exception.ToString(),
                    ErrorMessage = "Error perfoming string localization",
                });
                return QUERY;
            }
        }
    }
}
